import { UserContext } from "@/context/userContext";
import { Button } from "@ant-design/react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { useRouter } from "expo-router";
import { useContext, useEffect, useState } from "react";
import { Text } from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";

export default function MyScreen() {
  const router = useRouter();

  const { user } = useContext(UserContext);
  const [token, setToken] = useState<string | null>(null);

  const getToken = async () => {
    const token = await AsyncStorage.getItem("token");
    setToken(token);
  };

  useEffect(() => {
    getToken();
  }, []);

  console.log("MyScreen user:", user); // 添加调试信息

  return (
    <SafeAreaView>
      {token ? (
        <>
          <Text>用户名: {user?.username || "未登录"}</Text>
        </>
      ) : (
        <>
          <Button
            type="primary"
            onPress={() => {
              router.push("/sign_up");
            }}
          >
            注册
          </Button>
          <Button
            type="primary"
            onPress={() => {
              router.push("/sign_in");
            }}
          >
            登录
          </Button>
        </>
      )}
    </SafeAreaView>
  );
}
